Fix inconsistent ISO timestamp formatting causing ValueError on step update#2798
Fix inconsistent ISO timestamp formatting causing ValueError on step update#2798hztBUAA wants to merge 1 commit intoChainlit:mainfrom
Conversation
…update Timestamps were saved with trailing Z (UTC indicator) but read back via .isoformat() without Z, causing a ValueError when update_step tried to re-parse the createdAt string via strptime with the Z-requiring format. Changes: - Add _parse_iso_datetime() helper that handles both with/without Z - Add _datetime_to_utc_iso() helper that ensures trailing Z on output - Replace all .isoformat() calls with _datetime_to_utc_iso() - Replace strptime(created_at, ISO_FORMAT) with _parse_iso_datetime() - Switch datetime.now() to datetime.utcnow() for UTC consistency - Add tests covering parse/format roundtrip and step row conversion Fixes Chainlit#2491
|
Would love to merge this, please run |
dokterbob
left a comment
There was a problem hiding this comment.
Great contrib! Please make requested fixups and ensure only code related to the problem at hand is in there! (E.g. stuff described in the PR description.)
If more changes are needed, knowing WHY is essential!
|
|
||
| def test_parse_without_z_raises_on_bad_format(self): | ||
| """Test that invalid format still raises ValueError.""" | ||
| with pytest.raises(ValueError): |
There was a problem hiding this comment.
This is actually correct, please add comment telling ruff to ignore this (explaining that it's the exception from underlying code).
| mime=str(row["mime"]), | ||
| objectKey=str(row["objectKey"]), | ||
| mime=str(row["mime"]) if row.get("mime") else None, | ||
| objectKey=row.get("objectKey"), |
There was a problem hiding this comment.
Changes here seem unrelated to the problem at hand.
| "Failed to get read URL for element '%s': %s", | ||
| elem.get("id", "unknown"), | ||
| e, | ||
| ) |
There was a problem hiding this comment.
Not sure it's a good idea to just log general Exception. This definitely should not be part of a fix regarding date time conversion!
Summary
Fixes #2491
ChainlitDataLayersaves timestamps with a trailingZ(e.g.,2025-09-04T02:00:42.164000Z) viaISO_FORMAT, but reads them back using Python's.isoformat()which omits theZ(e.g.,2025-09-04T02:00:42.164000). Whenupdate_stepcallscreate_stepwith the read-backcreatedAtstring,strptimefails with:Changes
_parse_iso_datetime()helper that tolerates both with and without trailingZ_datetime_to_utc_iso()helper that ensures all datetime-to-string conversions consistently include the trailingZ.isoformat()calls with_datetime_to_utc_iso()across user, thread, and step serializationdatetime.strptime(created_at, ISO_FORMAT)with_parse_iso_datetime(created_at)increate_stepdatetime.now()todatetime.utcnow()inget_current_timestamp()andupdate_thread()for correct UTC semanticsTest plan
_parse_iso_datetimecorrectly parses both"...Z"and"..."formats_datetime_to_utc_isoalways produces strings ending withZcreate_stepValueErrorSummary by cubic
Fix inconsistent ISO timestamp handling to prevent ValueError during step updates. Standardizes UTC timestamps with a trailing Z and makes parsing accept both formats.
Written for commit 9b70738. Summary will update on new commits.